<?php
/*
	This file is part of PGallery; a FrogCMS plugin.

	PGallery is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	PGallery is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with PGallery.  If not, see <http://www.gnu.org/licenses/>.
*/


// Include required models
require_once('models/CategoryModel.php');
require_once('models/ImageModel.php');


// Plugin details
Plugin::setInfos(array(
    'id'          => 'pgallery',
    'title'       => 'PGallery',
    'description' => 'A photo/graphics/image gallery plugin which uses ImageMagick',
    'version'     => '1.0',
   	'license'     => 'AGPL',
	'author'      => 'Craig A Rodway',
    'website'     => 'http://m0php.net/',
    'update_url'  => 'http://www.madebyfrog.com/plugin-versions.xml',
    'require_frog_version' => '0.9.4'
));


// Create backend tab
Plugin::addController('pgallery', 'Gallery');


// Add page behaviour
Behavior::add('pgallery', 'pgallery/pgallery.php');




/**
 * Show an inline gallery on a page anywhere on the site
 *
 * @param	int		category_id		Category ID for images
 */
function pgallery_inline($category_id){
	
	global $__FROG_CONN__;
	
	// Get plugin settings (for paths, sizes)
	$settings = Plugin::getAllSettings('pgallery');
	
	$urlpath = preg_replace('/\\\/', '/', $settings['upload_path']) . '/';
	
	// Main query to fetch images in the category
	$sql = 'SELECT i.* 
			FROM pgallery_images i 
			LEFT JOIN pgallery_categories c ON i.category_id = c.id 
			WHERE category_id = ? OR c.slug = ?
			ORDER BY i.name ASC';
	
	// Run query
	$db = $__FROG_CONN__->prepare($sql);
	$query = $db->execute(array($category_id, $category_id));
	$items = $db->fetchAll(5);
	
	// Set the URL to load for each item
	foreach($items as &$item){
		// Set image paths
		foreach(array('small', 'medium', 'large') as $size){
			$paths[$size] = $urlpath . str_replace('#', $size, $item->filename);
		}
		$item->paths = $paths;
		unset($paths);
	}
	
	// Load the view
	echo new View('../../plugins/pgallery/views/front/inline', array(
		'items' => $items,
		'settings' => $settings,
	));
	
}


/* End of file: pgallery/index.php */